package com.google.code.openperfmon.monapi;

import java.io.IOException;
import java.net.InetAddress;

import org.apache.log4j.Logger;

import com.google.code.openperfmon.monapi.win.WinIcmp;

public class Icmp {
	private static Logger logger = Logger.getLogger(Icmp.class);
	protected Icmp() {}
	public static final Icmp instance;
	
	static {
		if (Platform.isWindows()) {
			instance = new WinIcmp();
		}
		else {
			instance = new Icmp();
		}
	}
	
	public PingResult ping(InetAddress address, int count, int timeout) {
		PingResult res = new PingResult();
		res.setTTL(0); //??
		long time = System.currentTimeMillis();
		try {
			if (address.isReachable(timeout)) {
				time = System.currentTimeMillis() - time;
				for (int i = 0; i < count; i++)	res.addReply(time);
			}
		}
		catch (IOException e) {
			logger.error(e);
		}
		return res;
	}
}
